<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class City extends Admin_Controller {

    function __construct(){

        parent::__construct();
        $this->admin_navigation->parent_active('pref');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('pref/citys_model');
        $this->load->model('pref/prefs_model');
    }

    function manager_city() {
        $columns = array(
                array(
                    'name' => 'ID',
                    'type' => 'id',
                    'index' => 'city_id',
                    'filter' => 'city_id',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'Quận huyện',
                    'type' => 'text',
                    'index' => 'name',
                    'filter' => 'name',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'Tỉnh thành',
                    'type' => 'select',
                    'index' => 'pref_name',
                    'filter' => 'pref_id',
                    'options' => $this->prefs_model->get_prefs_options(),
                ),
                array(
                    'name' => 'Trạng thái',
                    'type' => 'publish',
                    'index' => 'status',
                    'filter' => 'status',
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),

            );
        $this->grid->columns($columns);
        $this->grid->datasource('pref/citys_model','get_citys');
        $this->grid->set_title('Quản lý quận huyện');
        $this->grid->add_button('Thêm mới',admin_url('*/city/add_city'),'add');
        $this->grid->set_row_action('edit_city');
        $this->grid->total_rows($this->citys_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/city/delete_city'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/city/change_status_city'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_city() {
        $city_id = $this->input->get_param('id', false);
        $this->load->model('city/citys_model');
        if ($city_id) {
            $data = $this->citys_model->get_city($city_id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
                'id' => 'city_form',
                'action' => admin_url('*/city/edit_post_city' , array('id' => $city_id)),
                'enctype' => 'multipart/form-data',
                ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('name', 'text', array('label' => 'Quận huyện', 'name' => 'name', 'required' => true,));
        $this->form->add_filed('alias', 'text', array('label' => 'Alias', 'name' => 'alias', ));
        $this->form->add_filed('pref_id', 'select', array('label' => 'Tỉnh thành', 'name' => 'pref_id', 'required' => true, 'values'=> $this->prefs_model->get_prefs_options(),));
        $this->form->add_filed('Trạng thái', 'select', array(
                'label' => 'Trạng thái',
                'name' => 'status',
                'values'=>array('Tắt','Bật')));

        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_city() {
        $this->_forward('edit_city');
    }

    function delete_city() {
        $cids = explode(',', $this->input->get_param('city_id',false));
        $this->load->model('city/citys_model');
        foreach ($cids as $cid) {
            $this->citys_model->delete_city($cid);
        }
        $this->_redirect(admin_url('*/city/manager_city'));
    }

    function change_status_city() {
        $cids = explode(',', $this->input->get_param('city_id',false));
        $this->load->model('city/citys_model');
        foreach ($cids as $cid) {
            $this->citys_model->change_status_city($cid);
        }
        $this->_redirect(admin_url('*/city/manager_city'));
    }

    function edit_post_city() {
        $city_id = $this->input->get_param('id', false);
        $this->load->model('city/citys_model');
        if (($data = $this->input->post())) {

            if ($city_id) {
                $this->citys_model->update_city($data, $city_id);
            } else {
                $this->citys_model->insert_city($data);
            }
            $this->_redirect(admin_url('*/city/manager_city'));
        }
    }
}
?>